package com.dkm.biz.gl.repository.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.gl.domain.GlConfig;
import com.dkm.biz.gl.persistence.dao.GlConfigQueryDao;
import com.dkm.biz.gl.persistence.entity.GlConfigPo;
import com.dkm.biz.gl.repository.GlConfigRepository;
import com.dkm.biz.repository.AbstractRepository;

/**
 * 对象功能:系统参数配置 Repository接口的实现类
 * 开发公司:唯心六艺软件 - Craft6.cn
 * 开发人员:winston
 * 创建时间:2014-09-13 11:13:48
 */
@Repository
public class GlConfigRepositoryImpl extends AbstractRepository<String, GlConfigPo,GlConfig> implements GlConfigRepository{
	  
	@Resource
	private  GlConfigQueryDao glConfigQueryDao;

	public GlConfig newInstance() {
		GlConfigPo po = new GlConfigPo();
		GlConfig glConfig = SpringHelper.getBean(GlConfig.class);
		glConfig.setData(po);
		return glConfig;
	}

	public GlConfig newInstance(GlConfigPo po) {
		GlConfig glConfig = SpringHelper.getBean(GlConfig.class);
		glConfig.setData(po);
		return glConfig;
	} 
	
	@Override
	protected IQueryDao<String, GlConfigPo> getQueryDao() {
		return glConfigQueryDao;
	}

	public GlConfigPo getByConfigKey(String key) {
		List<GlConfigPo> glConfigPos= glConfigQueryDao.findByConfigKey(key);
		if(glConfigPos==null || glConfigPos.size()==0){
			return null;
		}else{
			return glConfigPos.get(0);
		}
	}
	
	public List<GlConfigPo> findByConfigKey(String key) {
		return glConfigQueryDao.findByConfigKey(key);
	}

	@Override
	public List<GlConfigPo> findBykeys(String[] keys) {
		Map<String, Object> paramsConfig = new HashMap<>();
		StringBuffer stringBuffer = new StringBuffer("key_ in(");
		for(String key : keys){
			stringBuffer.append("'"+key+"',");
		}
		String sql = stringBuffer.toString();
		sql = sql.substring(0, sql.length() -1);
		paramsConfig.put("whereSql", sql+")");
		return findAll(paramsConfig);
	}

	@Override
	public Map<String, String> getMapByConfigPo(List<GlConfigPo> glConfigPos) {
		Map<String, String> dataMap = new HashMap<>();
		for(GlConfigPo glConfigPo : glConfigPos){
			dataMap.put(glConfigPo.getKey(), glConfigPo.getValue());
		}
		return dataMap;
	}
	
}
